//
//  SuperMamViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/23.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SuperMamViewController.h"

@interface SuperMamViewController ()<UITableViewDataSource,UITableViewDelegate,ClickCellDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic,retain)NSMutableArray *imgArr;
@property(nonatomic ,retain)ShipuModel *sModel;
@property(nonatomic, retain)NSMutableArray *text;



@end

@implementation SuperMamViewController

- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [_imgArr release];
    [_sModel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[ UIColor whiteColor];
    [self data];
    [self creatView];
    [self loadMBProgress];
    self.navigationItem.title = @"达人";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];

}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}
//获取数据
-(void)data
{
    NSString *str = @"http://api.izhangchu.com/?size=20&page=1&methodName=TalentRecommend&version=4.3&";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr =  responseObject[@"data"][@"data"];
        self.dataArr = [ShipuModel modelHanderWithArray:arr];
        self.imgArr = [ NSMutableArray array];
        self.text = [ NSMutableArray array];
        //遍历数组
        for (NSDictionary *dic in arr)
        {
            NSArray *iArr = [ dic objectForKey:@"post"];
            _sModel =[[ ShipuModel alloc] init];
            _sModel.imageArr = [ NSMutableArray array];
            _sModel.textArr = [ NSMutableArray  array];
            //在model里定义一个数组 用来存放每个字典里的image
            //取到image的value值存放在数组中 给cell里传数组
            for (NSDictionary *dic1 in iArr) {
                [ _sModel.imageArr addObject: [dic1 objectForKey:@"image"]];
                //传id的数组
                [_sModel.textArr addObject:[dic1 objectForKey:@"id"]];
            }
          
            //把model添加给数组
            [self.imgArr addObject:_sModel];
            [self.text addObject:_sModel];
         
        }
        
        [_tableView reloadData];
        [self.hud removeFromSuperview];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    

}
-(void)creatView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor= [ UIColor whiteColor];
    
    [_tableView registerClass:[SuperManTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
}
#pragma mark -分区数为dataArr的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
#pragma mark -cell设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SuperManTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ShipuModel *m = _dataArr[ indexPath.row];
    cell.nick.text = m.nick;
    [cell.head sd_setImageWithURL:[NSURL URLWithString:m.head_img]];
    cell.content.text = m.declaration;
    //把存放的大图片数组传给model
    ShipuModel *m1 = _imgArr[indexPath.row];
    cell.model = m1;
    /**传id的数组*/
    ShipuModel *m2 = _text[indexPath.row];
    cell.textModel = m2;
    //设置代理人
    cell.delegate =  self;
    
   
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  200 *HEIGHT;
}
#pragma mark -协议传值的协议方法
-(void)clickCell:(NSString *)str
{
    SuperMandetailViewController *detail = [[ SuperMandetailViewController alloc] init];
    detail.str= str;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
